home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / SRS / client / src / exit.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-12  |  4.4 KB  |  235 lines

  1. /* functions to terminate program */
  2. /* ------------------------------ */
  3.  
  4. #include "headers.h" /* has all important stuff */
  5.  
  6. /* free structures and exit */
  7. void quit(int stat)
  8. {
  9.    int jumped; /* 1 if returning from longjmp() */
  10.     
  11.    jumped = setjmp(doquit);
  12.    if ((jumped == ERROR) && (errno != 0)) 
  13.       error("error with setjmp(): %s\n\n", strerror(errno));
  14.  
  15.    if (stat == INIT) return;
  16.  
  17.    silent = 0;
  18.  
  19.    if (debugging == 1)
  20.    {
  21.       (void)putchar('\n');
  22.       (void)write(dblogfd, "\n", 1);
  23.    }
  24.  
  25.    if (child == 1)
  26.    {
  27.       if (chpid == getpid())
  28.          debug("(in quit) I'm the child\n");
  29.  
  30.       else
  31.          debug("(in quit) I'm the parent\n");
  32.    }
  33.  
  34.    error("received a signal/error to abort.. now exiting\n\n");
  35.  
  36. /*
  37.    if (debugging == 1)
  38.    {
  39.       (void)putchar('\n');
  40.       (void)write(dblogfd, "\n", 1);
  41.    }
  42. */
  43.  
  44.    done = 1;
  45.  
  46.    /* ----------------------- */
  47.  
  48.    (void)signal(SIGHUP, SIG_IGN);
  49.  
  50.    (void)signal(SIGINT, SIG_IGN);
  51.    (void)signal(SIGTERM, SIG_IGN);
  52.  
  53.    (void)signal(SIGPIPE, SIG_IGN);
  54.  
  55.    (void)signal(SIGCHLD, SIG_IGN);
  56.  
  57.    (void)signal(SIGUSR1, SIG_IGN);
  58.    (void)signal(SIGUSR2, SIG_IGN);
  59.  
  60.    if (debugging != 1)
  61.    {
  62.       (void)signal(SIGILL, SIG_IGN);
  63.       (void)signal(SIGBUS, SIG_IGN);
  64.       (void)signal(SIGSEGV, SIG_IGN);
  65.  
  66.       (void)signal(SIGQUIT, SIG_IGN);
  67.    }
  68.  
  69.    /* ----------------------- */
  70.  
  71.    if (chpid != getpid()) 
  72.    {
  73.       if (connected == 1)
  74.       {
  75.          gotAlrm = 0, errors = 0, errno = 0;
  76.  
  77.          debug("sending server quit request\n");
  78.  
  79.          if (streaming) send_data("END STREAM\n");   
  80.          send_data("QUIT\n"); 
  81.       }
  82.  
  83.       debug("closing sockets\n");
  84.  
  85.       (void)fclose(confd);
  86.       if (sockfd > 0) (void)close(sockfd);
  87.       if (klogfd > 0) (void)close(klogfd);
  88.       if (funix > 0) (void)close(funix);
  89.  
  90.       if (spoolfd != NULL) (void)fclose(spoolfd);
  91.  
  92.       closeLogFiles();
  93.  
  94.       debug("freeing serverlist\n");
  95.       freeServList(), freeSharedMem();
  96.  
  97.    }
  98.  
  99.    if (getpid() != chpid)
  100.    {
  101.       doKills();
  102.  
  103.       if (debugging == 1)
  104.       {
  105.          (void)printf("\n%cinished closing...\n",
  106.                      (debugging != 1 ? 'F' : 'f'));
  107.  
  108.          (void)fprintf(stderr,
  109.                        "%cee %s/%s for the error log for errors (if any)..\n", 
  110.                        (debugging != 1 ? 'S' : 's'), SRSdir, ERRLOG);
  111.       }
  112.    }
  113.  
  114.    if (debugging == 1)
  115.    {
  116.       (void)putchar('\n');
  117.       (void)write(dblogfd, "\n", 1);
  118.    }
  119.  
  120.    if (getpid() != chpid)
  121.    {
  122.       (void)close(errlogfd), (void)fclose(errlogfd1);
  123.  
  124.       if (debugging == 1)
  125.       {
  126.          (void)close(dblogfd);
  127.          (void)fclose(dblogfd1);
  128.       }
  129.    }
  130.  
  131.    /* jumped == value passed to longjmp() */
  132.    if (jumped == 0) exit(stat);
  133.    else exit(jumped);
  134.    
  135.  
  136. /* --------------------------- */
  137.  
  138.  
  139. /* close the facility.priority log files we spooled locally */
  140. void closeLogFiles()
  141. {
  142.    register int i;
  143.  
  144.    for (i = 0; i <= curlogfd; i++) 
  145.    {
  146.       if (logs[i].fd > 0) (void)close(logs[i].fd);
  147.       if (logs[i].filename != NULL) free(logs[i].filename);
  148.    }
  149. }
  150.  
  151.  
  152. /* --------------------------- */
  153.  
  154.  
  155. /* free/detach shared memory */
  156. void freeSharedMem()
  157. {
  158.    int res;
  159.  
  160.    debug("detaching shared memory\n");
  161.  
  162.    res = shmdt(segptr);
  163.    if (res == ERROR) 
  164.        error("error detaching the shared memory: %s\n\n",
  165.              strerror(errno));
  166. }
  167.  
  168.  
  169. /* --------------------------- */
  170.  
  171.  
  172. /* free server list structures */         
  173. void freeServList()
  174. {
  175.    register int i;
  176.  
  177.    if (curServ != ERROR)
  178.       for (i = 0; i <= useServ ; i++)
  179.          if (servList[i].name != NULL) free(servList[i].name);
  180. }
  181.  
  182.  
  183. /* --------------------------- */
  184.  
  185.  
  186. /* kill the parent process */
  187. void doKills()
  188. {   
  189. #  if !defined(BSD) && !defined(SUN)
  190.    int res;
  191. #  endif
  192.  
  193. # if !defined(SUN) && !defined(BSD)
  194. #  ifdef _POSIX_SAVED_IDS
  195.    res = setuid(0);
  196. #  else
  197.    res = seteuid(0);
  198. #  endif
  199.  
  200.    if (res == ERROR)
  201.    {
  202.       error("error setting [e]uid: %s\n\n", strerror(errno));
  203.       quit(ERROR);
  204.    }
  205. # endif
  206.  
  207.    if ((child == 1) && (chpid > 0))
  208.    {
  209.       if (debugging == 1) 
  210.          (void)printf("[DEBUG (pid %d)]: "
  211.                       "killing the ping (pid %d) process\n",
  212.                       (int)getpid(), (int)chpid);
  213.  
  214.       kill(chpid, SIGTERM);
  215.    }
  216.  
  217. #  if !defined(SUN) && !defined(BSD)
  218.    if (pwd != NULL)
  219.    {
  220. #     ifdef _POSIX_SAVED_IDS
  221.       res = setuid(pwd->pw_uid);
  222. #     else
  223.       res = seteuid(pwd->pw_uid);
  224. #     endif
  225.  
  226.       if (res == ERROR)
  227.       {
  228.          error("error setting [e]uid: %s\n\n", strerror(errno));
  229.          quit(ERROR);
  230.       }
  231.    }
  232. #  endif
  233. }
  234.